//
//  LoginViewController.m
//  CoolRun
//
//  Created by MacMin on 16/3/29.
//  Copyright © 2016年 MacMin. All rights reserved.
//

#import "LoginViewController.h"
#import "XMPPFramework.h"

@interface LoginViewController ()<XMPPStreamDelegate>
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *userPasswordTextField;
- (IBAction)loginButton:(id)sender;
/**设置一个XMPPstream属性 */
@property(nonatomic,strong)XMPPStream *stream;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)loginButton:(id)sender {
    
    //生成xmppstream流来把账号和密码连接到服务器
    self.stream = [[XMPPStream alloc]init];
    //设置代理，监督连接服务器连接结果，在主队类中实现
    [self.stream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    //连接服务器，设置jid（主机域名，ip，端口）
    self.stream.hostName = @"124.207.192.18";
    self.stream.hostPort = 5222;
    //构建一个jid
    XMPPJID *jid = [XMPPJID jidWithUser:self.userNameTextField.text domain:@"tarena.com" resource:@"iphone"];
    self.stream.myJID = jid;
    //连接服务器
     NSError *error = nil;
    [self.stream connectWithTimeout:XMPPStreamTimeoutNone error:&error];
    if (error) {
        NSLog(@"error:%@",error);
    }
    
   
    
    
    
}
//以上代码有问题，由于点击登录按钮后，stream流拿着用户名去连接服务器，能够登录上去需要使用xmppstream代理的方法：didConnect didDisconnect,如果成功发送 密码，失败则是网页错误；密码发到服务器，如果密码对了,则发送在线消息，如果错了则打印错误的消息；
#pragma -mark XMPPStreamDelegate
//send password
-(void)xmppStreamDidConnect:(XMPPStream *)sender{
    //连接成功，发送密码
    NSError *error1 = nil;
    [self.stream authenticateWithPassword:self.userPasswordTextField.text error:&error1];
    if (error1) {
        NSLog(@"%@",error1);
    }
}
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error{
    NSLog(@"密码不对");
}
//authority success,send presence
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
    XMPPPresence *presence = [XMPPPresence presence];
    [self.stream sendElement:presence];
    NSLog(@"chenggong");
}







@end
